<?php
namespace app\api\controller;

use app\admin\model\CarBindLog;
use app\admin\model\CarBrand;
use app\common\controller\Api;
use app\admin\model\Car as CarModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use app\admin\model\Driver;
use app\admin\model\User;

class Car extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取车辆列表
     *
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $uid = $this->auth->id;
        $carModel = new CarModel();
        $list = $carModel->where('uid', $uid)->page($page, $limit)->select();
        foreach ($list as $key => $value) {
            $carBindLog = (new CarBindLog())->where('car_id', $value['id'])->where('status',1)->find();
            $list[$key]['bind'] = $carBindLog ? $carBindLog->toArray() : [];
            $carBrand = (new CarBrand())->where('id', $value['brand_id'])->find();
            $list[$key]['brand'] = $carBrand ? $carBrand->toArray() : [];
        }
        $count = $carModel->where('uid', $uid)->count();
        $data = [
            'count' => $count,
            'list' => $list
        ];
        $this->success('', $data);
    }

    /**
     * 创建车辆
     *
     * @return void
     */
    public function create()
    {
        $plate = $this->request->post('plate');
        $brand_id = $this->request->post('brand_id');
        $image = $this->request->post('image');
        $power_type = $this->request->post('power_type');
        $track_type = $this->request->post('track_type');
        $weight = $this->request->post('weight');
        $load_weight = $this->request->post('load_weight');
        $length = $this->request->post('length');
        $width = $this->request->post('width');
        $height = $this->request->post('height');
        $uid = $this->auth->id;
        if (!$plate || !$brand_id || !$image || !$power_type || !$track_type || !$weight || !$load_weight || !$length || !$width || !$height) {
            $this->error('参数错误');
        }
        $userInfo = $this->auth->getUserInfo();
        if ($userInfo['cate'] != 1) {
            $this->error('您不是车主，不能添加车辆');
        }
        $data = [
            'plate' => $plate,
            'brand_id' => $brand_id,
            'image' => $image,
            'power_type' => $power_type,
            'truck_type' => $track_type,
            'weight' => $weight,
            'load_weight' => $load_weight,
            'length' => $length,
            'width' => $width,
            'height' => $height,
            'uid' => $uid,
        ];
        $res = (new CarModel())->save($data);
        if ($res) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 修改车辆信息
     *
     * @return void
     */
    public function update()
    {
        $id = $this->request->post('id');
        $plate = $this->request->post('plate');
        $brand_id = $this->request->post('brand_id');
        $image = $this->request->post('image');
        $power_type = $this->request->post('power_type');
        $track_type = $this->request->post('track_type');
        $weight = $this->request->post('weight');
        $load_weight = $this->request->post('load_weight');
        $length = $this->request->post('length');
        $width = $this->request->post('width');
        $height = $this->request->post('height');
        $uid = $this->auth->id;
        if (!$id || !$plate || !$brand_id || !$image || !$power_type || !$track_type || !$weight || !$load_weight || !$length || !$width || !$height) {
            $this->error('参数错误');
        }
        $userInfo = $this->auth->getUserInfo();
//        if ($userInfo['cate'] != 1) {
//            $this->error('您不是车主，不能添加车辆');
//        }
        $data = [
            'plate' => $plate,
            'brand_id' => $brand_id,
            'image' => $image,
            'power_type' => $power_type,
            'truck_type' => $track_type,
            'weight' => $weight,
            'load_weight' => $load_weight,
            'length' => $length,
            'width' => $width,
            'height' => $height,
            'uid' => $uid,
        ];
        $res = (new CarModel())->where('id', $id)->update($data);
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 删除车辆
     *
     * @return void
     */
    public function delete()
    {
        $id = $this->request->post('id');
        $uid = $this->auth->id;
        if (!$id) {
            $this->error('参数错误');
        }
        $userInfo = $this->auth->getUserInfo();
        if ($userInfo['cate'] != 1) {
            $this->error('您不是车主，不能删除车辆');
        }
        $res = (new CarModel())->where('id', $id)->where('uid', $uid)->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 获取车辆品牌
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function getBrand()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $brandModel = new CarBrand();
        $list = $brandModel->page($page, $limit)->select();
        $count = $brandModel->count();
        $data = [
            'count' => $count,
            'list' => $list
        ];
        $this->success('', $data);
    }

    /**
     * 获取车辆绑定记录
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function getCarBindLogList()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $type = $this->request->get('type');
        $carBindLogModel = new CarBindLog();
        $where = [];
        $where['cate'] = $type;
        $list = $carBindLogModel->where($where)->page($page, $limit)->select();
        foreach ($list as $key => $value) {
            $list[$key]['user'] = (new User())->where('id', $value['uid'])->find()['avatar'];
            $list[$key]['car'] = (new CarModel())->where('id', $value['car_id'])->find();
            $list[$key]['driver'] = (new Driver())->where('id', $value['driver_id'])->find();
            //总单数
            $list[$key]['order_count'] = 0;
        }
        $count = $carBindLogModel->where($where)->count();
        $data = [
            'count' => $count,
            'list' => $list
        ];
        $this->success('', $data);
    }

    /**
     * 审核车主车辆绑定
     *
     * @return void
     */
    public function auditBindLog()
    {
        $id = $this->request->post('id');
        $type = $this->request->post('type');// 1同意  2拒绝
        $carBindLogModel = new CarBindLog();
        if ($type == 1) {
            $res = $carBindLogModel->where('id', $id)->update(['status' => 1, 'cate' => 1]);
        } else {
            $res = $carBindLogModel->where('id', $id)->update(['status' => 2]);
        }
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 获取司机详情
     *
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getDriverDetail()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $carBindLogModel = new CarBindLog();
        $driver_id = $carBindLogModel->where('id', $id)->value('driver_id');
        if (!$driver_id) {
            $this->error('司机不存在');
        }
        $model = new Driver();
        $driver = $model->where('id', $driver_id)->find();
        if (!$driver) {
            $this->error('司机不存在');
        }
        $data = [
            'driver' => $driver,
        ];
        $this->success('', $data);
    }

    /**
     * 解绑司机
     *
     * @return void
     */
    public function unbind()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数错误');
        }
        $carBindLogModel = new CarBindLog();
        $res = $carBindLogModel->where('id', $id)->update(['status' => 2]);
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 重新邀请司机
     *
     * @return void
     */
    public function reInvite()
    {
        $id = $this->request->post('id');
        $phone = $this->request->post('phone');
        if (!$id) {
            $this->error('参数错误');
        }
        $carBindLogModel = new CarBindLog();
        $res = $carBindLogModel->where('id', $id)->update(['status' => 0]);
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}